<template>
  <div>
    <Row>
      <Col span="24">
      <Card class="margin-bottom-10">
        <Form inline>
          <FormItem class="margin-bottom-0">
            <Select v-model="searchConf.game_hash" filterable remote :remote-method="getGameName" style="width:260px"
              placeholder='请选择游戏' :loading="nameLoading">
              <Option v-for="(option, index) in gameNameOption" :value="option.value" :key="index">{{ option.label }}
              </Option>
            </Select>
          </FormItem>
          <FormItem class="margin-bottom-0">
            <Select v-model="searchConf.type" filterable style="width:160px" :clearable="true" placeholder='请选择排行榜类型'>
              <Option v-for="(item, index) in typeList" :key="index" :value="item.value">{{ item.label }}</Option>
            </Select>
          </FormItem>
          <FormItem class="margin-bottom-0">
            <Select v-model="searchConf.hero_id" filterable style="width:160px" :clearable="true" placeholder='请选择角色'>
              <Option v-for="(item, index) in roleList" :key="index" :value="item.value">{{ item.label }}</Option>
            </Select>
          </FormItem>
          <FormItem class="margin-bottom-0">
            <div style="width:120%;display: flex;justify-content: space-around;">
              <Button type="primary" @click="search">{{ $t('find_button') }}/{{ $t('refresh_button') }}</Button>
              <Button type="warning" @click="download">导出</Button>
            </div>
          </FormItem>
        </Form>
      </Card>
      </Col>
    </Row>
    <Row style="margin-top: 15px">
      <Col span="24">
      <Card>
        <div>
          <Table :loading="listLoading" :columns="columnsList" :data="tableData" border disabled-hover />
        </div>
        <div class="margin-top-15" style="text-align: center">
          <Page :total="tableShow.listCount" :current="tableShow.currentPage" :page-size="tableShow.pageSize"
            @on-change="changePage" @on-page-size-change="changeSize" show-elevator show-sizer show-total />
        </div>
      </Card>
      </Col>
    </Row>
  </div>
</template>
<script>
import { getList, kickRole, exportList } from '@/api/game-rank'
import { getChoose } from '@/api/game-manager'
const editButton = (vm, h, currentRow, index) => {
  if (vm.buttonShow.edit) {
    return h('Poptip', {
      props: {
        confirm: true,
        title: '您确定要将该角色踢出排行榜吗?本次操作无法逆转，请谨慎使用',
        transfer: true
      },
      on: {
        'on-ok': () => {
          kickRole({
            role_id: currentRow.role_id,
            type: vm.searchConf.type,
            hero_id: currentRow.hero_id,
            game_hash: currentRow.game_hash
          }).then(response => {
            vm.$Message.success(response.data.msg)
            vm.getList()
          })
          currentRow.loading = false
        }
      }
    }, [
      h('Button', {
        style: {
          margin: '0 5px'
        },
        props: {
          type: 'error',
          placement: 'top',
          loading: currentRow.isDeleting
        }
      }, '踢出')
    ])
  }
}
export default {
  name: 'operate-order',
  data () {
    return {
      nameLoading: false,
      gameNameOption: [],
      fileNameType: '',
      fileNameRole: '',
      typeList: [
        {
          value: 'pt',
          label: '爬塔'
        },
        {
          value: 'power',
          label: '战斗力'
        },
        {
          value: 'lost',
          label: '迷惘之塔'
        },
        {
          value: 'guild',
          label: '公会伤害'
        }
      ],
      roleList: [
        // {
        //   value: 1001,
        //   label: '驭剑者'
        // },
        {
          value: 1002,
          label: '剑影（狐狸）'
        },
        {
          value: 1004,
          label: '炎刃（狼）'
        },
        {
          value: 1005,
          label: '灵剑师（虎）'
        },
        {
          value: 1006,
          label: '双弩游侠（猞猁）'
        }
      ],
      gameShow: {},
      columnsList: [
        {
          title: '序号',
          type: 'index',
          width: 65,
          align: 'center'
        },
        {
          title: '所属游戏',
          align: 'center',
          render: (h, params) => {
            this.getGameName()
            return h('span', this.gameShow[params.row.game_hash])
          },
          width: 120
        },
        {
          title: 'openid',
          align: 'center',
          key: 'openid',
          tooltip: true,
          minWidth: 280
        },
        {
          title: '用户昵称',
          align: 'center',
          key: 'nickname',
          minWidth: 140
        },
        {
          title: '贵族等级',
          align: 'center',
          key: 'vip',
          minWidth: 100
        },
        {
          title: '角色等级',
          align: 'center',
          key: 'role_level',
          minWidth: 100
        },
        {
          title: '战斗力',
          align: 'center',
          key: 'power',
          minWidth: 100
        },
        {
          title: '爬塔分数',
          align: 'center',
          key: 'score',
          minWidth: 120
        },
        {
          title: '操作',
          align: 'center',
          width: 170,
          render: (h, params) => {
            return h('div', [
              editButton(this, h, params.row, params.index)
            ])
          }
        }
      ],
      tableData: [],
      tableShow: {
        currentPage: 1,
        pageSize: 10,
        listCount: 0
      },
      searchConf: {
        game_hash: '',
        type: '',
        hero_id: ''
      },
      modalSetting: {
        show: false,
        loading: false,
        index: 0
      },
      buttonShow: {
        edit: true
      },
      listLoading: false,
      total: {},
      userInfo: {}
    }
  },
  created () {
    let vm = this
    vm.getList()
    vm.getGameName()
    vm.hasRule('Rank/kickRole').then(res => {
      vm.buttonShow.edit = res
    })
  },
  methods: {
    getGameName (value) {
      if (!this.gameNameOption.length) {
        getChoose({
          keyword: value
        }).then(response => {
          this.gameNameOption = response.data.data.list
          this.gameNameOption.forEach(i => {
            this.gameShow[i.value] = i.label
          })
        })
      }
    },
    changePage (page) {
      this.tableShow.currentPage = page
      this.getList()
    },
    changeSize (size) {
      this.tableShow.pageSize = size
      this.getList()
    },
    search () {
      this.tableShow.currentPage = 1
      this.getList()
    },
    download () {
      if (this.searchConf.game_hash === '' || this.searchConf.type === '' || this.searchConf.hero_id === '') {
        return
      }
      let d = new Date(new Date().getTime())
      let date = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds()
      let fileName = `排行榜-${date}.xlsx`
      exportList({
        ...this.searchConf
      }).then(response => {
        if (response.data.byteLength === 31) {
          this.$Message.error('导出失败,暂无数据')
          return
        }
        if (response.data.byteLength === 32) {
          this.$Message.error('导出失败,数据超过1000条,请联系管理员')
          return
        }
        const data = new Blob([response.data], { type: 'application/octet-stream' })
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          // IE workaround
          window.navigator.msSaveBlob(data, fileName)
        } else {
          // Other browsers
          const url = window.URL.createObjectURL(data)
          const link = document.createElement('a')
          link.href = url
          link.setAttribute('download', fileName)
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)
        }
      })
    },
    getList () {
      let vm = this
      let params = {
        page: vm.tableShow.currentPage,
        size: vm.tableShow.pageSize,
        ...vm.searchConf
      }
      vm.listLoading = true
      getList(params).then(response => {
        vm.tableData = response.data.data.list
        vm.total = response.data.data.total
        if (response.data.data.userInfo) {
          vm.userInfo = response.data.data.userInfo
        }
        vm.tableShow.listCount = response.data.data.count
        vm.listLoading = false
      })
    }
  }
}
</script>
